import type { ElectronAPI } from '../electron-types';

// IPC服务类，使用装饰器记录所有IPC调用
export class IPCService {
  private electronAPI: ElectronAPI;

  constructor(electronAPI: ElectronAPI) {
    this.electronAPI = electronAPI;
  }

  // AI聊天相关 - 使用详细日志
  async aiChat(message: string) {
    console.log('🔵 IPC Request: aiChat');
    return await this.electronAPI.aiChat(message);
  }

  async getChatHistory() {
    console.log('🔵 IPC Request: getChatHistory');
    return await this.electronAPI.getChatHistory();
  }

  async clearChatHistory() {
    console.log('🔵 IPC Request: clearChatHistory');
    return await this.electronAPI.clearChatHistory();
  }

  async getHistoryList() {
    console.log('🔵 IPC Request: getHistoryList');
    return await this.electronAPI.getHistoryList();
  }

  async loadSessionToChat(sessionId: string) {
    console.log('🔵 IPC Request: loadSessionToChat', sessionId);
    return await this.electronAPI.loadSessionToChat(sessionId);
  }

  // 提示词相关 - 使用简单日志
  async getPrompts() {
    console.log('🔵 IPC Request: getPrompts');
    return await this.electronAPI.getPrompts();
  }

  async switchPrompt(index: number) {
    console.log('🔵 IPC Request: switchPrompt', index);
    return await this.electronAPI.switchPrompt(index);
  }

  async abortChat() {
    console.log('🔵 IPC Request: abortChat');
    return await this.electronAPI.abortChat();
  }

  // 系统功能 - 使用静默日志（只记录错误）
  async helloWorld(name?: string) {
    return await this.electronAPI.helloWorld(name);
  }

  async getSystemInfo() {
    return await this.electronAPI.getSystemInfo();
  }

  async getCurrentTime() {
    return await this.electronAPI.getCurrentTime();
  }

  async systemCall(command: string) {
    return await this.electronAPI.systemCall(command);
  }

  // 事件监听器 - 直接透传，不需要装饰器
  onAiStreamStart(callback: (data: any) => void) {
    this.electronAPI.onAiStreamStart(callback);
  }

  onAiStreamContent(callback: (data: any) => void) {
    this.electronAPI.onAiStreamContent(callback);
  }

  onAiStreamComplete(callback: (data: any) => void) {
    this.electronAPI.onAiStreamComplete(callback);
  }

  onAiReasoning(callback: (data: any) => void) {
    this.electronAPI.onAiReasoning(callback);
  }

  onAiReasoningEnd(callback: () => void) {
    this.electronAPI.onAiReasoningEnd(() => {
      callback();
    });
  }

  onAiToolExecutionStart(callback: (data: any) => void) {
    this.electronAPI.onAiToolExecutionStart(callback);
  }

  onAiToolExecutionComplete(callback: (data: any) => void) {
    this.electronAPI.onAiToolExecutionComplete(callback);
  }

  onAiError(callback: (data: any) => void) {
    this.electronAPI.onAiError(callback);
  }

  removeAllListeners() {
    this.electronAPI.removeAllListeners();
  }
}

// 创建单例实例
let ipcServiceInstance: IPCService | null = null;

export function createIPCService(electronAPI: ElectronAPI): IPCService {
  if (!ipcServiceInstance) {
    ipcServiceInstance = new IPCService(electronAPI);
  }
  return ipcServiceInstance;
}

export function getIPCService(): IPCService | null {
  return ipcServiceInstance;
}
